#Precise control with format()

'This is a point: {}'.format(Point2D(1, 2))

class Point2D:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __str__(self):
        return '({}, {})'.format(self.x, self.y)
    
    def __repr__(self):
        return 'Point2D(x={}, y={})'.format(self.x, self.y)

    def __format__(self, f):
        return '[Formatted point: {}, {}, {}]'.format(self.x, self.y, f)

'This is a point: {}'.format(Point2D(1, 2))


#Formatting instructions for __format__()
class Point2D:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __str__(self):
        return '({}, {})'.format(self.x, self.y)
    
    def __repr__(self):
        return 'Point2D(x={}, y={})'.format(self.x, self.y)

    def __format__(self, f):
        if f == 'r':
            return '{}, {}'.format(self.y, self.x)
        else:
            return '{}, {}'.format(self.x, self.y)

'{}'.format(Point2D(1,2))
'{:r}'.format(Point2D(1,2))


#Forcing format() to use __repr__() or __str__()\
'{!r}'.format(Point2D(1,2))
'{!s}'.format(Point2D(1,2))


# The format() build-in function
import math
"{:.3f}".format(math.pi)
format(math.pi, ".3f")